{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import IsolationForest\n",
    "import sys\n",
    "sys.path.append('../../common/')\n",
    "from evaluator import *\n",
    "import numpy as np\n",
    "import os\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_datase(dataset):\n",
    "    folder = os.path.join(\"../../processed\", dataset)\n",
    "    if not os.path.exists(folder):\n",
    "        raise Exception(\"Processed Data not found.\")\n",
    "    loader = []\n",
    "    for file in [\"train\", \"test\", \"labels\"]:\n",
    "        loader.append(np.load(os.path.join(folder, f\"{file}.npy\")))\n",
    "    ## 准备数据\n",
    "    train_data = loader[0]\n",
    "    test_data = loader[1]\n",
    "    labels = loader[2][:,0].reshape(-1,1)\n",
    "    return train_data, test_data, labels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SWaT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data, test_data, labels = load_datase(\"SWaT\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train data shape : (99000, 51)\n",
      "test data shape: (89984, 51)\n",
      "anomaly num : 10951\n",
      "anomaly ratio : 0.12169941322901849\n"
     ]
    }
   ],
   "source": [
    "print(f\"train data shape : {train_data.shape}\")\n",
    "print(f\"test data shape: {test_data.shape}\")\n",
    "print(f\"anomaly num : {np.count_nonzero(labels == 1)}\")\n",
    "ratio = np.count_nonzero(labels == 1)/labels.shape[0]\n",
    "print(f\"anomaly ratio : {ratio}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "without adjust: \n",
      "{'f1': 0.6232257546531799, 'precision': 0.6232307546130667, 'recall': 0.6232307546130667}\n",
      "point adjust: \n",
      "{'f1': 0.7980179603623159, 'precision': 0.70812110872374, 'recall': 0.9140717734324977}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "model = IsolationForest(n_estimators=128, max_samples=4096,contamination=ratio, max_features=test_data.shape[1])  # contamination参数表示异常值的比例\n",
    "model.fit(test_data)\n",
    "predictions = model.predict(test_data)\n",
    "y_pred =  np.where(predictions == -1, 1, 0)\n",
    "p_t = calc_point2point(y_pred.squeeze(), labels.squeeze())\n",
    "print(\"without adjust: \")\n",
    "print({\n",
    "        'f1': p_t[0],\n",
    "        'precision': p_t[1],\n",
    "        'recall': p_t[2]\n",
    "})\n",
    "print(\"point adjust: \")\n",
    "y_pred = adjust_predicts(score=y_pred, label=labels, pred=y_pred)\n",
    "p_t = calc_point2point(y_pred.squeeze(), labels.squeeze())\n",
    "print({\n",
    "        'f1': p_t[0],\n",
    "        'precision': p_t[1],\n",
    "        'recall': p_t[2]\n",
    "})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WADI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data, test_data, labels = load_datase(\"WADI\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train data shape : (1209601, 123)\n",
      "test data shape: (172801, 123)\n",
      "anomaly num : 9860\n",
      "anomaly ratio : 0.0570598549776911\n"
     ]
    }
   ],
   "source": [
    "print(f\"train data shape : {train_data.shape}\")\n",
    "print(f\"test data shape: {test_data.shape}\")\n",
    "print(f\"anomaly num : {np.count_nonzero(labels == 1)}\")\n",
    "ratio = np.count_nonzero(labels == 1)/labels.shape[0]\n",
    "print(f\"anomaly ratio : {ratio}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "without adjust: \n",
      "{'f1': 0.29585178765400644, 'precision': 0.2958717919709182, 'recall': 0.29584178468981565}\n",
      "{'f1': 0.6775237822918223, 'precision': 0.5535691314768044, 'recall': 0.873022311487807}\n"
     ]
    }
   ],
   "source": [
    "model = IsolationForest(n_estimators=128, max_samples=4096,contamination=ratio, max_features=test_data.shape[1])  # contamination参数表示异常值的比例\n",
    "model.fit(test_data)\n",
    "predictions = model.predict(test_data)\n",
    "y_pred =  np.where(predictions == -1, 1, 0)\n",
    "p_t = calc_point2point(y_pred.squeeze(), labels.squeeze())\n",
    "print(\"without adjust: \")\n",
    "print({\n",
    "        'f1': p_t[0],\n",
    "        'precision': p_t[1],\n",
    "        'recall': p_t[2]\n",
    "})\n",
    "y_pred = adjust_predicts(score=y_pred, label=labels, pred=y_pred)\n",
    "p_t = calc_point2point(y_pred.squeeze(), labels.squeeze())\n",
    "print({\n",
    "        'f1': p_t[0],\n",
    "        'precision': p_t[1],\n",
    "        'recall': p_t[2]\n",
    "})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yjq-ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
